package team.gs.Messenger;

import team.gs.Listener.loginListener;
import team.gs.io.Message;
import team.gs.io.Session;
import team.gs.io.myIO;

public class Messenger {

	private loginListener loginListener;

	public Messenger() {

	}

	public void setLoginListener(loginListener lL) {
		this.loginListener = lL;
	}

	public void processMessage(Message message, Session session) {
		byte byte0 = message.getCmd();
		System.out.println("byte = " + byte0);
		switch (byte0) {
		case 0: // '\0'
			processMessage_request_login_from_Player(message, session);
			break;
		default:
			break;
		}
	}

	public boolean isValid(String s) {
		for (int k = 0; k < s.length(); k++) {
			char c1 = s.charAt(k);
			if ((c1 < '0' || c1 > '9') && (c1 < 'a' || c1 > 'z')
					&& (c1 < 'A' || c1 > 'Z')) {
				return false;
			}
		}

		return true;
	}

	private void processMessage_request_login_from_Player(Message message,
			Session session) {
		byte byte0 = myIO.readByte(message);
		if (byte0 == 1) {
			System.out.println("Dang nhap thanh cong");
			String result = myIO.readString(message);
			loginListener.resultOfLogging(true, result);
			System.out.println(result);

		} else {
			System.out.println("Dang nhap sai sot");
			String result = myIO.readString(message);
			System.out.println(result);
			loginListener.resultOfLogging(false, result);
		}
		loginListener.done(true);
	}
}
